var locationBarListener = {
  uri: null,

  QueryInterface: function(aIID)
  {
    if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
    aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
    aIID.equals(Components.interfaces.nsISupports))
    return this;
    throw Components.results.NS_NOINTERFACE;
  },

  onLocationChange: function(aProgress, aRequest, aURI)
  {
    //debug("locationBarListener.js::onLocationChange] aURI.spec:" + aURI.spec,4);
    var currentUri = aURI.spec;
    if(typeof(currentUri) != "string" || currentUri == ""
    || (currentUri.substring(0, "about:".length) == "about:")
    || (currentUri.substring(0, "chrome:".length) == "chrome:")
    || (currentUri.substring(0, "wyciwyg:".length) == "wyciwyg:"))
    return;

    if(this.uri == currentUri)
    return;

    //debug("[locationBarListener.js::onLocationChange] new Uri:" + currentUri, 4);
    this.uri = currentUri;
    sidebar.doRefreshDocument();
  },

  onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
  {
    var STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
    var STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
    var STATE_IS_DOCUMENT = Components.interfaces.nsIWebProgressListener.STATE_IS_DOCUMENT;
    /*debug("locationBarListener.js::onStateChange] document.location.href:" + aWebProgress.DOMWindow.document.location.href,4);
    debug("locationBarListener.js::onStateChange] aRequest.URI.asciiSpec:" + aRequest.URI.asciiSpec,4);
    debug("locationBarListener.js::onStateChange] STATE_START:" + STATE_START,4);
    debug("locationBarListener.js::onStateChange] STATE_STOP:" + STATE_STOP,4);
    debug("locationBarListener.js::onStateChange] STATE_IS_DOCUMENT:" + STATE_IS_DOCUMENT,4);
    debug("locationBarListener.js::onStateChange] aFlag:" + aFlag,4);
    debug("locationBarListener.js::onStateChange] aStatus:" + aStatus,4);
    */

    // If you use myListener for more than one tab/window, use
    // aWebProgress.DOMWindow to obtain the tab/window which triggers the state change
    if (aFlag & (STATE_IS_DOCUMENT|STATE_START))
    {
      debug("locationBarListener.js::onStateChange] load start.",4);
      //sidebar.docHtml = "";
      // This fires when the load event is initiated
      // TODO disable highlight menu
    }
    else if (aFlag & (STATE_IS_DOCUMENT|STATE_STOP))
      {
        var doc = aWebProgress.DOMWindow.document;
        var docHtml = doc.documentElement.innerHTML;
        if(docHtml.indexOf(HIGHLIGHT_TAG) > 0)
          return 0;
        sidebar.doPageLoad(docHtml);
        //debug("locationBarListener.js::onStateChange] load finished.",4);
        //debug("locationBarListener.js::onStateChange] aRequest.URI.asciiSpec:" + aRequest.URI.asciiSpec,4);
        //debug("locationBarListener.js::onStateChange] doc:\n" + doc.documentElement.innerHTML,4);
        //req.URI
        // This fires when the load finishes
        //if(this.uri == aRequest.URI)
        // sidebar.doPageLoad();
        //sidebar.showHighLighter();
      }
      return 0;
    },

    onProgressChange: function() {return 0;},
    onStatusChange: function() {return 0;},
    onSecurityChange: function() {return 0;},
    onLinkIconAvailable: function() {return 0;}
  };

  window.addEventListener("load", function(){
    var gBrowser = mainWindow.getBrowser();
    gBrowser.addProgressListener(locationBarListener,Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  }, true);
  window.addEventListener("unload", function(){
    var gBrowser = mainWindow.getBrowser();
    gBrowser.removeProgressListener(locationBarListener);
  }, true);